package gateway;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import form.Login;
import gateway.exception.CarpoolDBException;
import model.user.User;
import play.db.DB;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Tom
 * Date: 7-2-14
 * Time: 16:21
 */
public class DatabaseGateway {

    public void verifyUserLogin(Login login) throws CarpoolDBException, SQLException {
        Connection connection = openConnection();

        ResultSet result;
        try {
            PreparedStatement statement = connection.prepareStatement(
                    "select '' from USER where EMAIL = ? and PASSWORD = ?");
            statement.setString(1, login.getEmail());
            statement.setString(2, login.getPassword());
            result = statement.executeQuery();

            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }

        result.last();
        if (result.getRow() < 1){
            throw new CarpoolDBException("Invalid username or password.");
        }
    }


    public void insertUser(User user) throws CarpoolDBException, SQLException{
        Connection connection = openConnection();

        int result;
        try {
            PreparedStatement statement = connection.prepareStatement(
                    "insert into USER (EMAIL, PASSWORD, FIRST_NAME, LAST_NAME) values (?,?,?,?)");
            statement.setString(1, user.getEmail());
            statement.setString(2, user.getPassword());
            statement.setString(3, user.getFirst_name());
            statement.setString(4, user.getLast_name());
            result = statement.executeUpdate();

            connection.close();
        }  catch (MySQLIntegrityConstraintViolationException e){
            throw new CarpoolDBException("The email '" + user.getEmail() + "' is already in use.");
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }

        if (result != 1){
            throw new CarpoolDBException("An error has occurred, your account has not been created.");
        }
    }

    public void editPassword(String email, String new_password) throws CarpoolDBException, SQLException{
        Connection connection = openConnection();

        int result;
        try {
            PreparedStatement statement = connection.prepareStatement(
                    "update USER set PASSWORD=? where EMAIL=?");
            statement.setString(1, new_password);
            statement.setString(2, email);
            result = statement.executeUpdate();

            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }

        if (result != 1){
            throw new CarpoolDBException("An error has occurred, your password has not been updated.");
        }
    }


    protected Connection openConnection() throws CarpoolDBException {
        Connection connection = DB.getConnection();
        if (connection == null) throw new CarpoolDBException("Unable to create database connection.");
        return connection;
    }

}
